package com.example.wangyuyuan.custompopwindows;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wangyuyuan.custompopwindows.pop.CommonPopupWindow;
import com.example.wangyuyuan.custompopwindows.pop.CommonUtil;
import com.example.wangyuyuan.custompopwindows.pop.DpUtil;
import com.example.wangyuyuan.custompopwindows.pop.MyOnclickListener;
import com.example.wangyuyuan.custompopwindows.pop.PopupAdapter;

/**
 * 自定义popwindow，简单的调取方法，实现不同的弹出效果以及子控件的点击效果
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener, CommonPopupWindow.ViewInterface {

    private CommonPopupWindow popupWindow;
    private Button button2;//向下
    private Button button;//向右
    private Button tolift;//向左
    private Button fullscreen;//全屏
    private Button toup;//向上
    private ImageView image;//任意


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button2 = (Button) findViewById(R.id.button2);
        button = (Button) findViewById(R.id.button);
        tolift = (Button) findViewById(R.id.tolift);
        fullscreen = (Button) findViewById(R.id.fullscreen);
        toup = (Button) findViewById(R.id.toup);
        image = (ImageView) findViewById(R.id.image);
        button2.setOnClickListener(this);
        button.setOnClickListener(this);
        tolift.setOnClickListener(this);
        fullscreen.setOnClickListener(this);
        toup.setOnClickListener(this);
        image.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button2:
                if (popupWindow != null && popupWindow.isShowing()) return;
                popupWindow = new CommonPopupWindow.Builder(this)
                        .setView(R.layout.popup_down)
                        .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setAnimationStyle(R.style.AnimDown)
                        .setViewOnclickListener(this)
                        .setOutsideTouchable(true)
                        .create();
                popupWindow.showAsDropDown(view);
                //得到button的左上角坐标
//        int[] positions = new int[2];
//        view.getLocationOnScreen(positions);
//        popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, 0, positions[1] + view.getHeight());

                break;
            case R.id.button:
                popupWindow = new CommonPopupWindow.Builder(this)
                        .setView(R.layout.popup_left_or_right)
                        .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setAnimationStyle(R.style.AnimHorizontal)
                        .setViewOnclickListener(this)
                        .create();
                popupWindow.showAsDropDown(view, view.getWidth(), -view.getHeight());
                //得到button的左上角坐标
//        int[] positions = new int[2];
//        view.getLocationOnScreen(positions);
//        popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, positions[0] + view.getWidth(), positions[1]);

                break;
            case R.id.tolift:
                if (popupWindow != null && popupWindow.isShowing()) return;
                popupWindow = new CommonPopupWindow.Builder(this)
                        .setView(R.layout.popup_left_or_right)
                        .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setAnimationStyle(R.style.AnimRight)
                        .setViewOnclickListener(this)
                        .create();
                popupWindow.showAsDropDown(view, -popupWindow.getWidth(), -view.getHeight());
                //得到button的左上角坐标
//        int[] positions = new int[2];
//        view.getLocationOnScreen(positions);
//        popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, positions[0] - popupWindow.getWidth(), positions[1]);
                break;
            case R.id.fullscreen:
                if (popupWindow != null && popupWindow.isShowing()) return;
                View upView = LayoutInflater.from(this).inflate(R.layout.popup_up, null);
                //测量View的宽高
                CommonUtil.measureWidthAndHeight(upView);
                popupWindow = new CommonPopupWindow.Builder(this)
                        .setView(R.layout.popup_up)
                        .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, upView.getMeasuredHeight())
                        .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                        .setAnimationStyle(R.style.AnimUp)
                        .setViewOnclickListener(this)
                        .create();
                popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);

                break;
            case R.id.toup:
                if (popupWindow != null && popupWindow.isShowing()) return;
                popupWindow = new CommonPopupWindow.Builder(this)
                        .setView(R.layout.popup_left_or_right)
                        .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .setViewOnclickListener(this)
                        .create();
                popupWindow.showAsDropDown(view, 0, -(popupWindow.getHeight() + view.getMeasuredHeight()));

                //得到button的左上角坐标
//        int[] positions = new int[2];
//        view.getLocationOnScreen(positions);
//        popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.NO_GRAVITY, positions[0], positions[1] - popupWindow.getHeight());

                break;
            case R.id.image:
                if (popupWindow != null && popupWindow.isShowing()) return;
                popupWindow = new CommonPopupWindow.Builder(this)
                        .setView(R.layout.query_info)
                        .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                        .create();
                popupWindow.showAsDropDown(view, (int) (-popupWindow.getWidth() + DpUtil.dp2px(this, 20)), -(popupWindow.getHeight() + view.getMeasuredHeight()));

                break;
        }

    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            case R.layout.popup_down:
                RecyclerView recycle_view = (RecyclerView) view.findViewById(R.id.recycle_view);
                recycle_view.setLayoutManager(new GridLayoutManager(this, 3));
                PopupAdapter mAdapter = new PopupAdapter(this);
                recycle_view.setAdapter(mAdapter);
                mAdapter.setOnItemClickListener(new MyOnclickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Toast.makeText(MainActivity.this, "position is " + position, Toast.LENGTH_LONG).show();
//                        toast("position is " + position);
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                    }
                });
                break;
            case R.layout.popup_left_or_right:
                TextView tv_like = (TextView) view.findViewById(R.id.tv_like);
                TextView tv_hate = (TextView) view.findViewById(R.id.tv_hate);
                tv_like.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "赞一个 ", Toast.LENGTH_LONG).show();

//                        toast("赞一个");
                        popupWindow.dismiss();
                    }
                });
                tv_hate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "踩一下 ", Toast.LENGTH_LONG).show();

//                        toast("踩一下");
                        popupWindow.dismiss();
                    }
                });
                break;

            case R.layout.popup_up:
                Button btn_take_photo = (Button) view.findViewById(R.id.btn_take_photo);
                Button btn_select_photo = (Button) view.findViewById(R.id.btn_select_photo);
                Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
                btn_take_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        toast("拍照");
                        Toast.makeText(MainActivity.this, "拍照 ", Toast.LENGTH_LONG).show();
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                    }
                });
                btn_select_photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        toast("相册选取");
                        Toast.makeText(MainActivity.this, "相册选取 ", Toast.LENGTH_LONG).show();
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                    }
                });
                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                    }
                });
                view.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (popupWindow != null) {
                            popupWindow.dismiss();
                        }
                        return true;
                    }
                });
                break;
        }
    }
}
